package com.fanfutian.eventproject.controller;


import com.fanfutian.eventproject.pojo.Article;
import com.fanfutian.eventproject.pojo.PageBean;
import com.fanfutian.eventproject.pojo.Result;
import com.fanfutian.eventproject.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/article")
public class ArticleController {



    @Autowired
    private ArticleService articleService;

    /**
     * 新增文章
     * @param article
     * @return
     */
    @PostMapping("/addArticle")
    public Result addArticle(@RequestBody @Validated Article article){
        log.info("新增文章··········{}",article);

        articleService.addArticle(article);
        return Result.success();
    }




    /**
     * 文章列表查询
     * @return
     */
    @GetMapping("/list")
    public Result<PageBean<Article>> listArticle(Integer pageNum,
                                                 Integer pageSize,
                                                 @RequestParam(required = false) Integer categoryId,
                                                 @RequestParam(required = false) String state){
        log.info("查询文章列表(分页查询)··········查询条件:{}","pageNum="+pageNum+",pageSize="+pageSize+",categoryId="+categoryId+",state="+state);
        PageBean<Article> pageBean = articleService.list(pageNum,pageSize,categoryId,state);
        return Result.success(pageBean);
    }




    /**
     * 根据id查询文章详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result<Article> getArticleById(Integer id){
        log.info("根据id查询文章详情··········文章id：{}",id);
        Article article = articleService.getArticleById(id);
        return Result.success(article);
    }


    /**
     * 修改文章
     * @return
     */
    @PutMapping("/uploadArticle")
    public Result updateArticle(@RequestBody @Validated(Article.UpdateArticle.class) Article article){
        log.info("修改文章信息··········{}",article);
        articleService.updateArticle(article);
        return Result.success();
    }


    /**
     * 删除文章
     * @param id
     * @return
     */
    @DeleteMapping("/deleteArticle")
    public Result deleteArticle(Integer id){
        log.info("删除文章··········文章id：{}",id);
        articleService.deleteArticle(id);
        return Result.success();
    }
}
